/**
 *全局共用的函数模块
 **/

/*获取控制器的动作所对应的URL*/
function geturl(ctrl, act, pms)
{
	var p = '';
	var j;
    var m = parseInt(UrlMode);

	if(pms){
		for(j in pms){
			if(m == 1) p += '&' + j + '=' + pms[j];
			else if(m == 2) p += '/' + j + '/' + pms[j];
		}
	}

	if(m == 1) return (DomainDoor + '?' + UrlCtrlName + '=' + ctrl + '&' + UrlActName + '=' + act + p);
	else if(m == 2) return (DomainDoor + '/' + ctrl + '/' + act + p);
    
	return '';
}

/*页头滑动幕布式错误消息提示*/
function messageError(info, id)
{
	messageShow(info, 0, id);
	return true;
}

/*页头滑动幕布式正确消息提示*/
function messageSuccess(info, id)
{
	messageShow(info, 1, id);
	return true;
}

/*页头滑动幕布式消息提示*/
function messageShow(info, t, id)
{
	if(typeof(t) == 'undefined') t = 1;
	var msg = '<div id="mSgDivBOX" class="';
	if(t == 1) msg += 'message_success';
	else msg += 'message_error';
	msg += '"><div class="msgBody">'+info+'</div>';
	msg += '<div class="msgClose" onclick="messageClose()">&nbsp;&nbsp;</div>';
	msg += '</div>';
	messageRemove();
	if(id) $('#'+id).prepend(msg);
	else $('body:first').prepend(msg);
	$('#mSgDivBOX').show('normal');
	return true;
}

/*删除页头滑动幕布式消息*/
function messageRemove()
{
	$('#mSgDivBOX').remove();
	return true;
}

/*关闭页头滑动幕布式消息*/
function messageClose()
{
	$('#mSgDivBOX').hide('fast');
	return true;
}

/*正确信息提示*/
function right(info, backfunc)
{
	backfunc = typeof(arguments[1]) != 'undefined' ? arguments[1] : '';
	var alertTit = '<font color="white">正确提示</font>';
	info = '<div style="width:45px;padding:5px;float:left;"><img src="'+JsImgPath+'right.gif" align="absmiddle" /></div><div style="width:200px;margin:10px 0;font-size:12px;line-height:16px;color:#000">'+info+'</div>';
	alerting(info, alertTit, 270, 0, 0, backfunc);
	return true;
}

/*错误信息提示*/
function wrong(info, backfunc)
{
	backfunc = typeof(arguments[1]) != 'undefined' ? arguments[1] : '';
	var alertTit = '<font color="white">错误提示</font>';
	info = '<div style="width:45px;padding:5px;float:left;"><img src="'+JsImgPath+'warning.gif" align="absmiddle" /></div><div style="width:200px;margin:10px 0;font-size:12px;line-height:16px;color:#000">'+info+'</div>';
	alerting(info, alertTit, 270, 0, 0, backfunc);
	return true;
}

/*确认信息提示*/
function verify(info, backfunc)
{
	backfunc = typeof(arguments[1]) != 'undefined' ? arguments[1] : '';
	var alertTit = '<font color="white">确认提示</font>';
	info = '<div style="width:45px;padding:5px;float:left;"><img src="'+JsImgPath+'verify.gif" align="absmiddle" /></div><div style="width:200px;margin:10px 0;font-size:12px;line-height:16px;color:#000">'+info+'</div>';
	alerting(info, alertTit, 270, 0, 2, backfunc);
	return true;
}

/*浮动IFRAME调用一个页面*/
function floatPage(url, title, width, height, backfunc)
{
	backfunc = typeof(arguments[4]) != 'undefined' ? arguments[4] : '';
	var alertTit = '<font color="white">'+title+'</font>';
	alerting(url, alertTit, width, height, 1, backfunc);
	return true;
}

/*读取COOKIE中变量*/
function getCookie(objName)
{
	var arrStr = document.cookie.split("; ");
	for(var i = 0;i < arrStr.length;i ++){
		var temp = arrStr[i].split("=");
		if(temp[0] == objName) return decodeURIComponent(temp[1]).replace(/\+/g,' ');
	}
    return ''; 
}

/*设置Cookie*/
function setCookie(objName,objValue,objHours)
{
   var str = objName + "=" + escape(objValue);
   if(objHours > 0){//为0时不设定过期时间，浏览器关闭时cookie自动消失
		var date = new Date();
		var ms = objHours*3600*1000;
		date.setTime(date.getTime() + ms);
		str += "; expires=" + date.toGMTString();
   }
   document.cookie = str;
   return true;
}

/*删除Cookie*/
function delCookie(name)
{
   var date = new Date();
   date.setTime(date.getTime() - 10000);
   document.cookie = name + "=a; expires=" + date.toGMTString();
   return true;
}

/*读取URL中变量*/
function getVar(key)
{
	if(!window.location.search) return false;

	var query = window.location.search.substring(1) + '&';
	var tem = query.split("&");
	var arr = Array();
	var x;

	for(x in tem){
		if(typeof(tem[x]) == 'string'){
			var temp = tem[x] . split("=");
			var title = temp[0];
			var value = temp[1];
			arr[title] = value;
		}
	}

	var typeKey = typeof(arr[key]);

	if(key != ""){
		if(typeKey == 'undefined' || typeKey == 'unknown') return false;
		return arr[key];
	}
	return arr;
}

/*复选框全选(1)与反选(0),
参数name为复选框控件的名字
参数key为开关 全选(1)反选(0)
*/
function allSel(name, key)
{
	var sel_arr = document.getElementsByName(name);
	if(sel_arr != null){
		for(var i=0;i<sel_arr.length;i++){
			if(key == 1)
				sel_arr[i].checked = true;	
			if(key == 0)
				sel_arr[i].checked = false;	
		}
	}
	return true;
}

/*在鼠标位置显示浮动提示框*/
function showtips(obj, txt)
{
	var tips = document.createElement('div');
	var mn = getPageSize();
	tips.setAttribute('id', 'divTips');
	tips.innerHTML = '<span>'+txt+'</span>';

	//obj.style.cursor = 'pointer';
	obj.onmousemove = function(e){
		if(document.body) document.body.appendChild(tips);
		else if(document.documentElement) document.documentElement.appendChild(tips);
		var xy = getMouseXY(e);
		var ab = getElementWH(tips);
		var sl = getScorllXY();
		var tx,ty;

		if(xy[0] + 300 > mn[2] + sl[0]) tx = xy[0] - 278;
		else tx = xy[0] + 10;

		if(xy[1] + ab[1] + 10 > mn[3] + sl[1]){
			if(xy[0] + 300 > mn[2] + sl[0]) tx = xy[0] - 273;
			else tx = xy[0] + 2;
			ty = xy[1] - ab[1];
		}else ty = xy[1] + 10;

		tips.style.cssText = 'width:260px;word-break:break-all;word-wrap:break-word;padding:1px 5px;position:absolute;z-inde:99999;top:'+ty+'px;left:'+tx+'px;border:1px solid #786A01;background-color:#FFFF85;color:#000;line-height:18px;font-size:13px';
	}
	obj.onmouseout = function(){
		$('#divTips').remove();
		//document.body.removeChild(document.getElementById('divTips'));
	}
}

/*获取鼠标坐标值*/
function getMouseXY(e)
{
	e=e || window.event;		
	var x=e.pageX;
	var y=e.pageY; 
	if(x == undefined) x = e.clientX;
	if(y == undefined) y = e.clientY;

	var sxy = getScorllXY();
	if($.browser.msie){
		x += parseInt(sxy[0], 10);
		y += parseInt(sxy[1], 10);
	}
	return [x, y];
}

/*获取滚动条坐标值*/
function getScorllXY()
{
	if(document.documentElement){
		var x = document.documentElement.scrollLeft;
		var y = document.documentElement.scrollTop;
	}else if(document.body){
		var x = document.body.scrollLeft;
		var y = document.body.scrollTop;
	}
	return [x, y];
}

/*获取元素的左上坐标值及宽和高*/
function getElementXY(obj)
{ 
	var x = obj.offsetLeft; 
	var y = obj.offsetTop; 
	var w = obj.offsetWidth;
	var h = obj.offsetHeight;
	while(obj = obj.offsetParent) 
	{ 
		x += obj.offsetLeft; 
		y += obj.offsetTop; 
	}
	return [x, y, w, h]; 
}

/*获取元素的宽和高*/
function getElementWH(obj)
{
	var w = obj.offsetWidth;
	var h = obj.offsetHeight;
	return [w, h];
}

/*准确获得页面、窗口高度及宽度 */
function getPageSize(){
	var xScroll, yScroll;
	if (window.innerHeight && window.scrollMaxY) {
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	var windowWidth, windowHeight;
	if (self.innerHeight) { // all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else {
		pageHeight = yScroll;
	}
	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}
	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight)
	return arrayPageSize;
}

/*
浮动显示，不基于JQUERY
a:内容，可以是一个页面地址
b:标题
d:宽度
f:高度
e:内容是否为一个页面地址，0:否   1:是 (不带滚动条) 2:须确认  3:是(带滚动条) 
g:回调函数
*/
function alerting(a, b, d, f, e, g)
{
	if(!d) d = 220;
	if(!f) f = 80;
	if(!e) e = 0;
	
	//定义样式
	var hei,bg,bd,bgl,bgr,cls,dls;
	cls = JsImgPath+'close.gif';//关闭图标
	dls = 'width:52px;height:22px;line-height:20px;border:0;background:url('+JsImgPath+'button_nor.gif) no-repeat;color:#333;margin:10px;font-size:12px;';//按钮样式
	hei = 25;//标题栏高度
	bgl = 'url('+JsImgPath+'tipl.gif) no-repeat';//标题栏左侧圆角背景，5px
	bgr = 'url('+JsImgPath+'tipr.gif) no-repeat';//标题栏右侧圆角背景，5px
	bg = 'url('+JsImgPath+'tipm.gif) repeat-x';//标题栏中间背景，变长
	bd = '1px solid #051885';//信息正文的边框	

	var msg = '<TABLE border="0" cellspacing="0" cellpadding="0" style="width:'+d+'px;border-collapse:separate;">';
	msg += '<TR><TD><TABLE border="0" cellspacing="0" cellpadding="0" width="100%">';
	msg += '<TR id="DtipsTit" valign="middle" align="left">';
	msg += '<TD id="DtipsTit_tl"></TD>';
	msg += '<TD id="DtipsTit_t">'+b+'&nbsp;&nbsp;</TD>';
	msg += '<TD id="C_alert"><a title="关闭">×</a></TD>';
	msg += '<TD id="DtipsTit_tr"></TD>';
	msg += '</TR></TABLE></TD></TR>';
	msg += '<TR><TD id="DtipsCont">';

	msg += '<TABLE border="0" cellspacing="2" cellpadding="0" width="100%"><TR><TD>';
	if(e == 1 || e == 3){
		msg += '<iframe id="ShW_Iframe" frameborder="0" src="'+a+'" width="100%" height="'+f+'" scrolling="';
		if(e == 1)  msg += 'no"></iframe></TD></TR>'; 
		if(e == 3)  msg += 'yes"></iframe></TD></TR>'; 
	}
	else msg += a+'</TD></TR>';
	if(e == 0) msg += '<TR><TD align="center"><INPUT type="button" id="D_alert" value="确 定"></TD></TR>';
	if(e == 2) msg += '<TR><TD align="center"><INPUT type="button" id="D_alert" value="确 定">&nbsp;&nbsp;&nbsp;<INPUT type="button" id="Q_alert" value="取 消"></TD></TR>';
	msg += '</TABLE></TD></TR>';
	msg += '</TABLE>';

	TINY.box.show(msg);	
	//var FLOATbox = new TINYbox;
	//FLOATbox.show(msg);
	var TINYbar = new Draggable("TinyBox0", 'DtipsTit');
	var dd = (parseInt(d, 10)-12)+'px';

	document.getElementById('DtipsTit_tl').style.cssText = 'width:5px;height:'+hei+'px;background:'+bgl;
	document.getElementById('DtipsTit_tr').style.cssText = 'width:5px;background:'+bgr;
	document.getElementById('DtipsTit_t').style.cssText = 'width:'+dd+';height:'+hei+'px;line-height:'+hei+'px;font-weight:bold;font-size:13px;cursor:move;background:'+bg;
	document.getElementById('C_alert').style.cssText = 'width:12px;color:#fff;font-size:20px;font-weight:bold;cursor:pointer;background:'+bg;
	document.getElementById('DtipsCont').style.cssText = 'width:'+d+'px;height:'+f+'px;line-height:20px;border:'+bd+';border-top:0;padding:0px;font-size:13px;background:#fff;overflow:hidden;';
		
	if(e != 1 && e!= 3) document.getElementById('D_alert').style.cssText = dls;
	if(e == 2) document.getElementById('Q_alert').style.cssText = dls;

	document.getElementById('C_alert').innerHTML = '<a title="关闭"><img src="'+cls+'" /></a>';	
	
	if(e != 2){
		if(e != 0){
			document.getElementById('ShW_Iframe').src = document.getElementById('ShW_Iframe').src;//TMD，IE6不刷新下，出不来。
		}else{
			document.getElementById('D_alert').onclick = function(){
				TINYbar.destroy();
				TINY.box.hide();
				if(g != '') setTimeout(g, 1);
			}
		}
		document.getElementById('C_alert').onclick = function(){
			TINYbar.destroy();
			TINY.box.hide();
			if(g != '') setTimeout(g, 1);
		}
	}else{
		document.getElementById('D_alert').onclick = function(){
			TINYbar.destroy();
			//TINY.box.hide();
			if(g != '') setTimeout(g, 1);
		}
		document.getElementById('C_alert').onclick = function(){
			TINYbar.destroy();
			TINY.box.hide();
		}
		document.getElementById('Q_alert').onclick = function(){
			TINYbar.destroy();
			TINY.box.hide();
		}
	}
	
}


/*UTF-8字符串截取*/
function substrcn(str, len)
{
    if(!str || !len) { return ''; }
    var a = 0;
    var i = 0;
    var temp = '';

    for (i=0; i<str.length; i++)
    {
        if (str.charCodeAt(i)>255) a += 1;
        else a++;
        if(a > len) return temp + '...';
        temp += str.charAt(i);
    }

    return str;
}

/*HTML编码*/
function decodeHtml(html)
{
    html = html.replace(/&lt;/g, '<');
    html = html.replace(/&gt;/g, '>');
    html = html.replace(/&amp;/g, '&');
    html = html.replace(/&nbsp;/g, ' ');
    return html
}

/*URL解码*/
function encodeHtml(html)
{
    html = html.replace(/&/g, '&amp;');
    html = html.replace(/</g, '&lt;');
    html = html.replace(/>/g, '&gt;');
    html = html.replace(/ /g, '&nbsp;');
    return html
}

/* AJAX提交表单数据 */
function ajaxSubmit(obj)
{
    var bef = $(obj).attr('check');
    if(bef == '' || typeof(bef) == 'undefined') bef = 'formCheck';
    var suc = $(obj).attr('success');
    if(suc == '' || typeof(suc) == 'undefined') suc = 'formResult';
    var err = $(obj).attr('error');
    if(err == '' || typeof(err) == 'undefined') err = 'formResult';
    var ty = $(obj).attr('datatype');
    if(ty == '' || typeof(ty) == 'undefined') datatype = 'html';
    var tm = $(obj).attr('timeout');
    if(tm == '' || typeof(tm) == 'undefined') tm = '20000';
    var act = $(obj).attr('action');
    if(act == '' || typeof(act) == 'undefined') act = location.href;
    var mod = $(obj).attr('method');
    if(mod == '' || typeof(mod) == 'undefined') mod = 'post';
    
    var m = eval(bef+'()');
    
    if(m){
        $.ajax({
            url: act,
            type: mod,
            data: $(obj).serialize(),
            dataType: ty,
            timeout: tm,
            success: eval(suc),
            error: eval(err)   
        });
    }
   
    return false;
}

/* AJAX提交表单前数据检查 */
function formCheck()
{
    return true;
}

/* AJAX提交表单结果处理 */
function formResult(res, st)
{
    if(st == 'success') alert('ajaxSubmitSuccess:'+res);
    else alert('ajaxSubmitError:'+res);
    return true;
}


